package com.smarttiger.zero.admin.service;

import com.smarttiger.zero.admin.dao.ArgsDao;
import com.smarttiger.zero.admin.dao.LogicDao;
import com.smarttiger.zero.admin.dto.LogicDto;
import com.smarttiger.zero.admin.entity.LogicModel;
import com.smarttiger.zero.admin.util.Constant;
import com.smarttiger.zero.admin.util.Message;
import com.smarttiger.zero.admin.util.ParamNames;
import com.smarttiger.zero.admin.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class LogicService extends BaseService<LogicModel> {
    @Autowired
    LogicDao logicDao;

    @Autowired
    ArgsDao argsDao;

    @PostConstruct
    public void initMethod() {
        super.setDao(logicDao);
    }

    @ParamNames("name,page")
    public Message getLogics(String name, String page) {
        Message message = new Message();
        Pageable pageable = PageRequest.of(Integer.valueOf(page).intValue(), Constant.PAGESIZE, Sort.by(Sort.Direction.DESC,"createDate"));
        Page<LogicModel> all = logicDao.findAll(new Specification<LogicModel>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<LogicModel> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(name)) {
                    predicates.add(criteriaBuilder.like(root.get("name"), name + "%"));
                }
                criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate")));
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        },pageable);
        long totalElements = all.getTotalElements();
        message.setData(LogicDto.tranlateList(all.getContent()));
        message.setTotal((int)totalElements);
        return message;
    }

    @ParamNames("id")
    public LogicModel getLogicById(String id) {
        LogicModel logicModel = logicDao.findById(id).get();
        logicModel.setArgsModels(argsDao.findAllByLogicId(id));
        return logicModel;
    }

    @ParamNames("interfaceId")
    public List<LogicDto> findLogicsByInterfaceId(String interfaceId) {
        List<LogicModel> logicModels = logicDao.findLogicModelByInterfaceId(interfaceId);
        return LogicDto.tranlateList(logicModels);
    }

    @Override
    @Transactional
    public void delete(String id) {
        super.delete(id);
        argsDao.deleteByLogicId(id);
    }

    @ParamNames
    public Object getLogicMaps(){
        Object logicMaps = SpringContextUtils.getBean("logicMaps");
        return logicMaps;
    }


}
